﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using WMS.HelperClasses;

namespace WMS.Reports
{
    public partial class SupplierAnalysisReportForm : Form
    {
        public SupplierAnalysisReportForm()
        {
            InitializeComponent();
        }

        private void BtnSupplierClick(object sender, EventArgs e)
        {
            var searchForm = new FrmGeneralMultiSelecteItem("Supplier");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtSupplier.Text = string.Join(",", searchForm.SelectedItemCodes);
            }
            EnableDisableControls();
        }

        private void BtnStoreClick(object sender, EventArgs e)
        {
            var searchForm = new FrmGeneralMultiSelecteItem("Store");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtStoreCode.Text = string.Join(",", searchForm.SelectedItemCodes);
            }
            EnableDisableControls();
        }

        private void BtnItemsClick(object sender, EventArgs e)
        {
            var searchForm = new FrmGeneralMultiSelecteItem("Item");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtProductCode.Text = string.Join(",", searchForm.SelectedItemCodes);
            }
            EnableDisableControls();
        }

        private void SupplierTxtChanged(object sender, EventArgs e)
        {
            EnableDisableControls();
        }

        private void StoreTxtChanged(object sender, EventArgs e)
        {
            EnableDisableControls();
        }

        private void ProductTxtChanged(object sender, EventArgs e)
        {
            EnableDisableControls();
        }

        private void EnableDisableControls()
        {
            btnRun.Enabled = !string.IsNullOrEmpty(txtProductCode.Text) &&
                             !string.IsNullOrEmpty(txtStoreCode.Text) &&
                             !string.IsNullOrEmpty(txtSupplier.Text);
        }

        private void BtnGenerateReport(object sender, EventArgs e)
        {
            bindingSource1.DataSource = Business.GetSupplierAnalysisReport(txtSupplier.Text, txtStoreCode.Text,
                                                                           txtProductCode.Text,
                                                                           datePickerFrom.Value, datePickerTo.Value);

            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", bindingSource1));

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("Suppliers", txtSupplier.Text));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("Stores", txtStoreCode.Text));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("Products", txtProductCode.Text));

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("FromDate", datePickerFrom.Value.ToShortDateString()));
            this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ToDate", datePickerTo.Value.ToShortDateString()));

            this.reportViewer1.RefreshReport();
        }

        private void SupplierAnalysisReportForm_Load(object sender, EventArgs e)
        {

        }
    }
}
